#include  <head.h>

typedef struct shm_str
{
    int id;
    char name[128];
}shm_str;


int main(int argc, char const *argv[])
{
    int shmid;
    shm_str * shm_ptr = NULL;

    key_t key = ftok("./key",'A');

    //创建共享内存
    shmid = shmget(key,4096,IPC_CREAT|0666);
    if(shmid==-1)
    {
        printf("共享内存创建失败\n");
        return 0;
    }    

    //内存映射 将共享内存 映射到当前进程
    shm_ptr = (shm_str *)shmat(shmid,NULL,0);
    if(shm_ptr==(void *)-1)
    {
        ERRLOG("共享内存映射失败");
        exit(0);
    }

    //读取结构体
    printf("id = %d, name = %s\n",(*shm_ptr).id,shm_ptr->name);

    //解除内存映射
    shmdt(shm_ptr);
    
    return 0;
}
